<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="70%" v-dialog-drag append-to-body>
      <div style="padding: 5px 10px; margin-bottom: 20px;">
        <el-row>
          <el-col :span="5">
            任务编号：{{ pickTask.code }}
          </el-col>
          <el-col :span="5">
            状态：{{ getDictDataLabel(DICT_TYPE.PICK_TASK_STATUS, pickTask.status) }}
          </el-col>
          <el-col :span="5">
            分配给：{{ pickTask.assignUserName }}
          </el-col>
          <el-col :span="5">
            数量：{{ startPick.count }}
          </el-col>
          <el-col :span="4">
            已拣数量：{{ startPick.pickCountTotal }}
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column type="index" label="序号" align="center" width="50"/>
        <el-table-column label="托盘编码" align="center" prop="trayCode"/>
        <el-table-column label="RFID编码" align="center" prop="rfidCode"/>
        <el-table-column label="物料代码" align="center" prop="materialModeCode"/>
        <el-table-column label="入库时间" align="center" prop="warehousingTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.warehousingTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="需求数量" align="center" prop="count">
          <template v-slot="scope">
            <span>{{ scope.row.pickCount }} / {{ scope.row.count }}</span>
          </template>
        </el-table-column>
        <el-table-column label="指示灯" align="center" prop="lightName"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="scope">
            <el-button v-show="scope.row.pickCount < scope.row.count" size="mini" type="text" icon="el-icon-full-screen" @click="openPickScanForm(scope.row)"
                       v-hasPermi="['hs:pick-task-material:update']">扫码拣货</el-button>
            <el-button v-show="scope.row.pickCount >= scope.row.count" size="mini" type="text" icon="el-icon-finished" disabled
                       v-hasPermi="['hs:pick-task-material:update']">已拣完</el-button>
<!--            <el-button size="mini" type="text" icon="el-icon-info" @click="openPickTaskMaterialDetailList(scope.row)"-->
<!--                       v-hasPermi="['hs:pick-task-material:query']">待拣明细</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="pickedList.length > 0" type="primary" @click="openPickMergeForm" size="small">合 托</el-button>
        <el-button v-if="pickedList.length === 0 && confirmDone" type="primary" @click="confirm" size="small">确认全部拣完</el-button>
      </div>
    </el-dialog>
    <PickScanForm ref="pickScanFormRef" @success="getList" />
    <StartPickMaterialDetailList ref="startPickMaterialDetailListRef" @success="getList" />
    <PickMergeForm ref="pickMergeFormRef" @success="getList" />
  </div>
</template>
<script>
import PickScanForm from "@/views/hs/componnets/PickScanForm.vue";
import StartPickMaterialDetailList from "@/views/hs/componnets/StartPickMaterialDetailList.vue";
import {complete, getPickTask, getStartPick} from "@/api/hs/picktask";
import PickMergeForm from "@/views/hs/componnets/PickMergeForm.vue";
import {getPickedList} from "@/api/hs/scan";

export default {
  name: "StartPick",
  components: {StartPickMaterialDetailList, PickScanForm, PickMergeForm},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 物料信息列表
      list: [],
      // 拣货任务信息
      startPick: {},
      // 拣货任务
      pickTask: {},
      // 判断是否全部拣完
      pickedList: [],

      confirmDone: false,
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(pickTaskId) {
      this.reset();
      this.dialogVisible = true;
      this.dialogTitle = "开始拣货"
      await this.getList(pickTaskId);
    },
    /** 查询列表 */
    async getList(pickTaskId) {
      try {
        this.loading = true;
        const pickTaskRes = await getPickTask(pickTaskId);
        this.pickTask = pickTaskRes.data;
        const res = await getStartPick(pickTaskId);
        this.startPick = res.data;
        this.list = res.data.list;
        let flg = false;
        this.list.forEach(item => {
          if (item.light !== 3) {
            flg = true;
          }
        })
        this.confirmDone = !flg;
        await this.getPickList(pickTaskId);
      } finally {
        this.$emit('success');
        this.loading = false;
      }
    },
    /** 打开扫码拣货对话框 */
    openPickScanForm(row) {
      this.$refs["pickScanFormRef"].open(row);
    },
    openPickMergeForm() {
      this.$refs["pickMergeFormRef"].open(this.pickTask.id);
    },
    getPickList(pickTaskId) {
      this.formLoading = true;
      getPickedList(pickTaskId).then(res => {
        if (res.code === 0) {
          this.pickedList = res.data;
        }
      }).finally(() => {
        this.formLoading = false;
      });
    },
    reset() {
      this.list = [];
      this.pickTask = {};
      this.startPick = {};
    },
    async confirm() {
      const res = await complete(this.pickTask.id);
      if (res.code === 0) {
        this.$message.success("拣货任务执行成功");
        this.dialogVisible = false;
        this.$emit("success");
      }
    }
  }
}
</script>
<style scoped lang="scss">

</style>
